package com.soft.mainsite.module.city.service;

import com.soft.mainsite.module.city.dao.CityDao;
import com.soft.mainsite.module.city.dto.CityDto;
import com.soft.mainsite.module.city.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * CityServiceImpl.java 此类用于城市业务层
 *
 * @author:李昭阳
 * @date:2019/4/12 0012
 * @remark:
 */
@Component
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;

    /**
     * 只读事务标识，读写分离
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public List<CityDto> getProviceList() {
        List<City> cities = cityDao.selectProviceList();
        List<CityDto> cityDtos = CityDto.convertEntityToDtos(cities);
        return cityDtos;
    }

    /**
     * 只读事务标识，读写分离
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public List<CityDto> getProviceCityList() {
        List<City> cities = cityDao.selectProviceCityList();
        List<CityDto> cityDtos = CityDto.convertEntityToDtos(cities);
        return cityDtos;
    }

    /**
     * 只读事务标识，读写分离
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public List<CityDto> getCityList() {
        return cityDao.selectCityList();
    }

    /**
     * 只读事务标识，读写分离
     * @param proviceCode
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public List<CityDto> getCityListByProvice(Integer proviceCode) {
        List<City> cities = cityDao.selectCityListByProvice(proviceCode);
        List<CityDto> cityDtos = CityDto.convertEntityToDtos(cities);
        return cityDtos;
    }

    /**
     * 只读事务标识，读写分离
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public List<CityDto> getSiteCityList() {
        // CmsConst.MAIN_SITE_ID
        // 原来是排除主站使用 现在不排除了  默认写0
        List<CityDto> cityDtos = cityDao.selectSiteCityList(0);
        return cityDtos;
    }

    /**
     * 只读事务标识，读写分离
     * @param siteId
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public CityDto getCityBySiteId(Integer siteId) {
        return cityDao.selectCityBySiteId(siteId);
    }

    /**
     * 根据城市父编码查询城市区县列表
     *
     * @param cityCode
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public List<City> getCityDistrictByCityParentCode(Integer cityCode){
        return cityDao.getCityDistrictByCityParentCode(cityCode);
    }
}
